<?php
namespace app\index\controller;
use think\Controller;
use think\Db;

class AnnualIncome extends Controller
{
    public function index(){
        return $this->fetch();
    }
    

    public function getAnnualIncome(){
        //实现功能：获取所有个人信息并整合到学院
        $issueDateList = Db('issue_date')->select();
        $companyList = Db('company')->select();
        $incomeList = array();
        foreach ($issueDateList as $issueDate){
            foreach ($companyList as $company){
                //查看当前日期和公司的发放人数，如果0人跳过
                $total_people = Db('person_annual_wage')
                    ->join('base_info','person_annual_wage.user_id = base_info.user_id')
                    ->where([
                        'base_info.company_id' => $company['id'],
                        'person_annual_wage.wage_date' => $issueDate['issue_date']
                    ])
                    ->count(1);
                if($total_people != 0){
                    $income = Db('person_annual_wage')
                        ->join('base_info','person_annual_wage.user_id = base_info.user_id')
                        ->where([
                            'base_info.company_id' => $company['id'],
                            'person_annual_wage.wage_date' => $issueDate['issue_date']
                        ])
                        ->sum('job_salary+job_level_salary+retention_benefit_subsidy+job_reward_performance_salary+basic_performance_award+special_post_allowance+property_subsidy+civilization_award+one_child_fee+ethnic_minorities_subsidy+public_sanitation_fee+doctor_subsidy+professor_subsidy+rental_subsidy+car_supplement+supplementary_salary');
                    $deduction = Db('person_annual_wage')
                        ->join('base_info','person_annual_wage.user_id = base_info.user_id')
                        ->where([
                            'base_info.company_id' => $company['id'],
                            'person_annual_wage.wage_date' => $issueDate['issue_date']
                        ])
                        ->sum('housing_provident_fund+medical_insurance_fund+endowment_insurance_individual_enterprise+endowment_insurance_individual+occupational_annuity_individual+deduction_of_individual_income_tax+deduction_of_rent+withholding_property+deduction_of_hydropower+deduction_of_wages');
                    $separately = Db('person_annual_wage')
                        ->join('base_info','person_annual_wage.user_id = base_info.user_id')
                        ->where([
                            'base_info.company_id' => $company['id'],
                            'person_annual_wage.wage_date' => $issueDate['issue_date']
                        ])
                        ->sum('heating_fee+target_assessment_award+national_civilized_city_award+merit_pay');
                    $incomeItem = array(
                        'wage_date' => $issueDate['issue_date'],
                        'company' => $company['company'],
                        'income' => $income,
                        'deduction' => $deduction,
                        'separately' => $separately,
                        'total_income' => $income+$separately-$deduction,
                        'total_people' => $total_people
                    );
                    array_push($incomeList,$incomeItem);
                }
            }
        }
        $msg = [
            'code' => 0,
            'msg' => '查询成功',
            'data' => $incomeList,
            'count' => count($incomeList)
        ];
        echo json_encode($msg);
    }
    
}
